<?php
namespace Home\Controller;
use Think\Page;

class AboutController extends BaseController
{
    const TOP_CAT_ID = 1;

    public function __construct()
    {
        parent::__construct();
        $cat1_id = I('get.cat1_id', 0, 'intval');
        $cat_list = $this->getCatList($cat1_id);
        $this->assign('cat1_id', $cat1_id);
        $this->assign('cat_list', $cat_list);
    }

    private function _getDefaulCatId()
    {
        $id = 0;
        $cat1_id = I('get.cat1_id', 0, 'intval');
        $list = M('SinglePageCategory')->where(array('parent_id' => $cat1_id))->select();
        if ($list) {
            $info = $list[0];
            $id = $info['id'];
        }
        return $id;
    }

    public function detailAction()
    {
        $defaultCatId = $this->_getDefaulCatId();
        $cat_id = I('GET.cat_id', $defaultCatId, 'intval');

        $page_info = array();

        //如果有子类,取第一个子类显示
        $condition = array(
            'parent_id' => $cat_id,
        );
        $cat_list = M('SinglePageCategory')->where($condition)->select();
        if (!empty($cat_list)) {
            $cat_id = $cat_list['0']['id'];
        }
        $condition = array(
            'cat_id' => $cat_id,
        );
        $page_list = M('SinglePage')->where($condition)->order('id desc')->select();
        if ($page_list) {
            $page_info = $page_list[0];
        }
        $global_config = array(
            'title' => $page_info['title'] . '-' . C('SITE_NAME'),
            'keywords' => $page_info['title'],
            'description' => $page_info['title'],
        );
        $this->assign('global_config', $global_config);
        $this->assign('cat_id', $cat_id);
        $this->assign('page_info', $page_info);
        $this->display();
    }

    public function contactUsAction()
    {
        if (IS_POST) {
            $Linkus = D('Linkus');
            $data = $Linkus->create($_POST['Linkus']);
            if (false !== $data) {
                $insertId = $Linkus->add();
                if (false !== $insertId) {
                    $this->success('提交成功');
                } else {
                    $this->error('提交失败');
                }
            } else {
                $this->error($Linkus->getError());
            }
        } else {
            $area_list = array('黄浦区', '徐汇区', '长宁区', '静安区', '普陀区', '虹口区', '杨浦区', '宝山区', '闵行区', '嘉定区',
                '浦东新区', '松江区', '金山区', '青浦区', '奉贤区', '崇明县',
            );
            $this->assign('area_list', $area_list);
            $this->display();
        }
    }

    /**
     * 课程报名
     */
    public function courseApplyAction()
    {
        //菜单选项
        $cat_id = I('GET.cat_id', 0, 'intval');
        $this->assign('cat_id', $cat_id);
        if (IS_POST) {
            $CourseApply = D('CourseApply');
            $data = $CourseApply->create($_POST['CourseApply']);
            if (false !== $data) {
                $insertId = $CourseApply->add();
                if (false !== $insertId) {
                    $this->success('提交成功');
                } else {
                    $this->error('提交失败');
                }
            } else {
                $this->error($CourseApply->getError());
            }
        } else {
            $course_list = M('Course')->select();
            $this->assign('course_list', $course_list);
            $this->display();
        }
    }

    /**
     * 岗位报名
     */
    public function jobApplyAction()
    {
        //菜单选项
        $cat_id = I('GET.cat_id', 0, 'intval');
        $this->assign('cat_id', $cat_id);
        if (IS_POST) {
            $JobApply = D('JobApply');
            $data = $JobApply->create($_POST['JobApply']);
            if (false !== $data) {
                $insertId = $JobApply->add();
                if (false !== $insertId) {
                    $this->success('提交成功');
                } else {
                    $this->error('提交失败');
                }
            } else {
                $this->error($JobApply->getError());
            }
        } else {
            $job_list = M('Job')->select();
            $this->assign('job_list', $job_list);
            $this->display();
        }
    }

    /**
 * 绿色营养餐餐普
 */
    public function mealAction()
    {
        //菜单选项
        $cat_id = I('GET.cat_id', 0, 'intval');
        $this->assign('cat_id', $cat_id);

        //搜索处理
        $condition = array();
        $duration = I('get.duration', '', 'trim');
        list($start_date, $end_date) = explode('至', $duration);
        if (!empty($start_date)) {
            $start_date = date('Y-m-d 00:00:00', strtotime($start_date));
            $condition['start_date'] = array('egt', $start_date);
        }
        if (!empty($end_date)) {
            $end_date = date('Y-m-d 23:59:59', strtotime($end_date));
            $condition['end_date'] = array('elt', $end_date);
        }
        $params['duration'] = $duration;
        $this->assign('params', $params);

        $Meal = D('Meal');
        $MealCategory = D('MealCategory');
        $count = $Meal->where($condition)->count();
        $Page = new Page($count, 2);
        $Page->rollPage = 5;
        $Page->setConfig('header', '');
        $Page->setConfig('prev', '上一页');
        $Page->setConfig('next', '下一页');
        $Page->setConfig('last', '');
        $show = $Page->show();
        $list = $Meal->where($condition)->order('id desc')->limit($Page->firstRow . ',' . $Page->listRows)->select();
        foreach($list as $key => $item) {
            $list[$key]['cat_list'] = $MealCategory->getCatList($item['id']);
        }
        $this->assign('page', $show);
        $this->assign('list', $list);
        $global_config = array(
            'title' => '绿色营养餐餐普-' . C('SITE_NAME'),
            'keywords' => '绿色营养餐餐普',
            'description' => '绿色营养餐餐普',
        );
        $this->assign('list', $list);
        $this->assign('global_config', $global_config);
        $this->display();
    }

    /**
     * 个性化食疗餐普
     */
    public function physiqueMealAction()
    {
        //菜单选项
        $cat_id = I('GET.cat_id', 0, 'intval');
        $this->assign('cat_id', $cat_id);

        //搜索处理
        $condition = "1=1";
        $keywords = I('get.keywords', '', 'trim');
        if (!empty($keywords)) {
            $keywords = addslashes($keywords);
            $condition .= " AND (`title` LIKE '%{$keywords}%' OR `description` LIKE '%{$keywords}%')";
        }
        $params['keywords'] = $keywords;
        $this->assign('params', $params);

        $Physique = D('Physique');
        $MealCategory = D('MealCategory');
        $count = $Physique->where($condition)->count();
        $Page = new Page($count, 2);
        $Page->rollPage = 5;
        $Page->setConfig('header', '');
        $Page->setConfig('prev', '上一页');
        $Page->setConfig('next', '下一页');
        $Page->setConfig('last', '');
        $show = $Page->show();
        $list = $Physique->where($condition)->order('id desc')->limit($Page->firstRow . ',' . $Page->listRows)->select();
        foreach($list as $key => $item) {
            $list[$key]['cat_list'] = $MealCategory->getCatList($item['id']);
        }
        $this->assign('page', $show);
        $this->assign('list', $list);
        $global_config = array(
            'title' => '个性化食疗餐-' . C('SITE_NAME'),
            'keywords' => '个性化食疗餐',
            'description' => '个性化食疗餐',
        );
        $this->assign('list', $list);
        $this->assign('global_config', $global_config);
        $this->display();
    }
}